package com.atgm.mall.gmmallclub.service.impl;

import com.alibaba.fastjson.TypeReference;
import com.atgm.common.exception.BizCodeEnume;
import com.atgm.common.exception.ClubException;
import com.atgm.common.utils.GmMallUtils;
import com.atgm.common.utils.PageUtils;
import com.atgm.common.utils.Query;
import com.atgm.common.utils.R;
import com.atgm.common.vo.member.MemberInfoVo;
import com.atgm.mall.gmmallclub.dao.GameCommentDao;
import com.atgm.mall.gmmallclub.entity.GameCommentEntity;
import com.atgm.mall.gmmallclub.feign.MemberFeignService;
import com.atgm.mall.gmmallclub.service.GameCommentService;
import com.atgm.mall.gmmallclub.vo.resp.GameCommentRespVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Collectors;

@Slf4j
@Service
public class GameCommentServiceImpl extends ServiceImpl<GameCommentDao, GameCommentEntity>  implements GameCommentService {

    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;

    @Autowired
    @Qualifier(value = "memberFeignService")
    private MemberFeignService memberFeignService;


    @Override
    public PageUtils queryCommentByGameId(Map<String, Object> params) {
        List<GameCommentRespVo> outList = new ArrayList<>();
        //获取游戏id
        Long gameId = Long.valueOf((String) params.get("game_id")) ;
        if (gameId == null) {
            throw new ClubException("入参[game_id]为null");
        }
        QueryWrapper<GameCommentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("game_id", gameId);
        IPage<GameCommentEntity> iPage = this.page(new Query<GameCommentEntity>().getPage(params),
                queryWrapper);
        List<GameCommentEntity> records = iPage.getRecords();

        if (GmMallUtils.isEmpty(records)) {
            return null;
        }
        records.forEach(gameCommentEntity -> {
            GameCommentRespVo gameCommentRespVo = new GameCommentRespVo();
            BeanUtils.copyProperties(gameCommentEntity, gameCommentRespVo);
            outList.add(gameCommentRespVo);
        });
        //得到会员信息
        List<Long> memberIdList = records.stream().map(GameCommentEntity::getMemberId).collect(Collectors.toList());
        R r = memberFeignService.getMemberInfo(memberIdList);
        List<MemberInfoVo> memberInfoVoList = null;
        if (0 == r.getCode()) {
            memberInfoVoList = r.getData(new TypeReference<List<MemberInfoVo>>() {});
        }
        if (GmMallUtils.isNotEmpty(memberInfoVoList)) {
            List<MemberInfoVo> finalMemberInfoVoList = memberInfoVoList;
            outList.forEach(gameCommentRespVo -> {
                gameCommentRespVo.setNickname(finalMemberInfoVoList.stream().filter(memberInfoVo -> {
                    return memberInfoVo.getId().equals(gameCommentRespVo.getMemberId());
                }).findAny().get().getNickname());
                gameCommentRespVo.setHeader(finalMemberInfoVoList.stream().filter(memberInfoVo -> {
                    return memberInfoVo.getId().equals(gameCommentRespVo.getMemberId());
                }).findAny().get().getHeader());
            });
        }

        PageUtils pageUtils = new PageUtils(iPage);
        pageUtils.setList(outList);
        //通过
        return pageUtils;
    }

    @Override
    public void updateThumbsCount(Long id, Integer count) {
        QueryWrapper<GameCommentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id", "useful_count");
        queryWrapper.eq("id", id);
        GameCommentEntity gameCommentEntity = baseMapper.selectOne(queryWrapper);
        if (gameCommentEntity != null) {
            if (0 == count) {  //-1
                gameCommentEntity.setUsefulCount(gameCommentEntity.getUsefulCount() - 1);
            }else {
                gameCommentEntity.setUsefulCount(gameCommentEntity.getUsefulCount() + 1);
            }
        }

        int update = baseMapper.updateById(gameCommentEntity);
        if (update == 0) {
            throw new ClubException("点赞失败", BizCodeEnume.MEMBER_THUMBS.getCode());
        }
    }
}
